package com.itedge.solutionmanager.service.task.impl.customer;

import org.activiti.engine.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itedge.solutionmanager.domain.impl.Address;
import com.itedge.solutionmanager.domain.impl.ContactPerson;
import com.itedge.solutionmanager.domain.impl.Customer;
import com.itedge.infrastructure.service.domain.IEntityService;
import com.itedge.infrastructure.service.domain.IProcessEntityService;
import com.itedge.infrastructure.service.task.ITaskService;
import com.itedge.infrastructure.service.taskdata.ITaskDataEntityService;
import com.itedge.solutionmanager.taskdata.impl.customer.AddContactPersonTaskData;

@Service
public class AddContactPersonTaskService implements ITaskService<AddContactPersonTaskData, Customer> {

    @Autowired
    protected TaskService taskService;
    @Autowired
    private IProcessEntityService<Customer> customerService;
    @Autowired
    private IEntityService<ContactPerson> contactPersonService;
    @Autowired
    private ITaskDataEntityService<AddContactPersonTaskData> addContactPersonTaskDataService;
	
	@Override
	@Transactional
	public void completeTask(String taskId, Customer linkedObject,
			AddContactPersonTaskData taskData, String user) {
    	addContactPersonTaskDataService.merge(taskData);
    	ContactPerson customerContact = new ContactPerson();
        customerContact.setFirstName(taskData.getContactPersonFirstName());
    	customerContact.setSecondName(taskData.getContactPersonSecondName());
    	customerContact.setTitle(taskData.getContactPersonTitle());
    	Address customerAddress = new Address();
    	customerAddress.setTelNumber(taskData.getContactPersonPhone());
    	customerContact.setAddress(customerAddress);
    	customerContact.setNotes(taskData.getContactPersonNotes());  
    	contactPersonService.persist(customerContact);   
    	linkedObject.addContactPerson(customerContact);
    	customerService.merge(linkedObject);
    	taskService.setAssignee(taskId, user);
    	taskService.complete(taskId);
	}

}
